package LeetCode.month01.day30;

public class No2516 {
    public int takeCharacters(String s, int k) {
        int[] cnt = new int[3];
        int n = s.length();
        char[] c = s.toCharArray();
        for (int i = 0; i < n; i++) {
            cnt[c[i]-'a']++;
        }
        if(cnt[0]<k||cnt[1]<k||cnt[2]<k) return -1;
        int left = 0;
        int res = 0;
        for (int i = 0; i < n; i++) {
            int index = c[i]-'a';
            cnt[index]--;
            while(cnt[index]<k){
                cnt[c[left]-'a']++;
                left++;
            }
            res = Math.max(res,i-left+1);
        }
        return n-res;
    }
}
